#include "control.h"
#include "ui_control.h"
#include "ui_rtc.h"
#include <QMessageBox>
#include <QDebug>
#include <string>
#include <QTimer>
#include <QCoreApplication>


int tcp_state =0;
QSerialPort *serial;
QTcpSocket *qtcpsocket;
QByteArray time_now;
QByteArray getcmd;
QByteArray TCP_buff;
int adc_state =0,count = 0;
int sd_read_flag;


control::control(QWidget *parent) :
   QWidget(parent),
   ui(new Ui::control)
{
    ui->setupUi(this);
    time = new rtc(this);
    gpiox = new gpio(this);
    adcx = new adc(this);
    timpwmx = new TIMPWM(this);
    canx = new can(this);
    ap3216c = new ap3216(this);
    sdflash = new sd_flash(this);

    serial = new QSerialPort(this);
    //轮询查看本机串口的容器,添加到下拉框
    foreach(const QSerialPortInfo &infor, QSerialPortInfo::availablePorts())
    {
        ui->usart_name->addItem(infor.portName());
    }

    connect(serial,SIGNAL(readyRead()),this,SLOT(read_data()));
    connect(this,SIGNAL(rng_change()),this,SLOT(rng_play()));
    connect(this, SIGNAL(tim_change()), time, SLOT(time_play()));
    connect(this, SIGNAL(adc_change()), adcx, SLOT(adc_play()));
    connect(this, SIGNAL(count_change()), timpwmx, SLOT(count_play()));
    connect(this, SIGNAL(can_change()), canx, SLOT(can_play()));
    connect(this, SIGNAL(ap3216c_change()), ap3216c, SLOT(ap3216c_ALS_play()));
    connect(this, SIGNAL(ap3216c_change()), ap3216c, SLOT(ap3216c_IR_play()));
    connect(this, SIGNAL(ap3216c_change()), ap3216c, SLOT(ap3216c_PS_play()));
    connect(this, SIGNAL(sdflash_change()), sdflash, SLOT(sdflash_play()));



    qtcpsocket= new QTcpSocket(this);
    //   发送信号,连接一起
     connect(qtcpsocket, SIGNAL(readyRead()), this, SLOT(receivemessage()));
     connect(qtcpsocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState )));


}




control::~control()
{
    delete ui;
}

void control::delay(int msec)
{
    QTimer tim;
    QEventLoop loop;
    QObject::connect(&tim, &QTimer::timeout, &loop, &QEventLoop::quit);
    tim.setSingleShot(true);
    tim.start(msec);
    loop.exec();
}

int rtc_flag;
void control::read_data()
{
    static int rng=0,can_flag = 0,ap3216c_flag=0,sdflah_flag=0;
    getcmd =serial->readAll();
    qDebug() << getcmd << endl;


    if(strstr(getcmd,"@rngOK .")!=NULL)
    {
      rng = 1;
    }

    if(strstr(getcmd,"@sd_flashOK .")!=NULL)
    {
      sdflah_flag = 1;
    }

    if(strstr(getcmd,"@rtcOK .")!=NULL)
    {
      rtc_flag=1;
    }

    if(strcmp(getcmd,"@adcOK .")==0)
     {
      adc_state=1;
      //rng=0;
     }

    if(strstr(getcmd,"@timOK .")!=NULL)
    {
      count=1;
    }

    if(strcmp(getcmd,"@canOK .")==0)
    {
      can_flag=1;
    }
    if(strcmp(getcmd,"@ap3216cOK .")==0)
    {
      ap3216c_flag=1;
    }
    if(rng == 1)
    {
     emit rng_change();
    }


    if(rtc_flag == 1)
    {
      //显示时间
      emit tim_change();
    }

    if(adc_state == 1)
    {
      emit adc_change();
    }

    if(count == 1)
    {
      emit count_change();
    }

    if(can_flag == 1)
    {
       emit can_change();
    }

    if(ap3216c_flag == 1)
    {
       emit ap3216c_change();
    }
    if(sdflah_flag == 1)
    {
       emit sdflash_change();
    }
       delay(10);


}



void control::receivemessage()
{

    static int rng=0,can_flag = 0,ap3216c_flag=0,tsdflah_flag=0;
    qDebug()<<"TCP" << TCP_buff << endl;
    TCP_buff = qtcpsocket->readAll();

    if(strstr(TCP_buff,"@rngOK .")!=NULL)
    {
      rng = 1;
    }

    if(strstr(TCP_buff,"@sd_flashOK .")!=NULL)
    {
      tsdflah_flag = 1;
    }

    if(strstr(TCP_buff,"@rtcOK .")!=NULL)
    {
      rtc_flag=1;
    }

    if(strstr(TCP_buff,"@adcOK .")!=NULL)
     {
      adc_state=1;
      //rng=0;
     }

    if(strstr(TCP_buff,"@timOK .")!=NULL)
    {
      count=1;
    }

    if(strcmp(TCP_buff,"@canOK .")==0)
    {
      can_flag=1;
    }
    if(strcmp(TCP_buff,"@ap3216cOK .")==0)
    {
      ap3216c_flag=1;
    }
    if(rng == 1)
    {
     emit rng_change();
    }


    if(rtc_flag == 1)
    {
      //显示时间
      emit tim_change();
    }

    if(adc_state == 1)
    {
      emit adc_change();
    }

    if(count == 1)
    {
      emit count_change();
    }

    if(can_flag == 1)
    {
       emit can_change();
    }

    if(ap3216c_flag == 1)
    {
       emit ap3216c_change();
    }

    if(tsdflah_flag == 1)
    {
       emit sdflash_change();
    }
       delay(10);


}


void control::stateChanged(QAbstractSocket::SocketState state)
{
    switch(state)
    {
    case QAbstractSocket :: UnconnectedState:
        tcp_state=0;

        break;
    case QAbstractSocket :: ConnectedState:
        tcp_state=1;

        break;
    default:
        break;
    }
}

void control::on_pushButton_clicked(bool checked)
{
    if(checked ==1)
    {
        //端口号为扫描到的串口号
        serial->setPortName(ui->usart_name->currentText());
        //设置波特率
        serial->setBaudRate(ui->usart_bute->currentText().toInt());
        //设置停止位
        serial->setStopBits(QSerialPort::StopBits( ui->usart_stop->currentText().toInt()));

        //设置数据位
        serial->setDataBits(QSerialPort::DataBits(ui->usart_datalengh->currentText().toInt()));
        //设置校验位
        switch(ui->usart_check->currentIndex())
        {
        case 0:
            serial->setParity(QSerialPort::Parity(0));
            break;
        case 1:
            serial->setParity(QSerialPort::Parity(5));
            break;
        case 2:
            serial->setParity(QSerialPort::Parity(2));
            break;
        case 3:
            serial->setParity(QSerialPort::Parity(3));
            break;
        case 4:
            serial->setParity(QSerialPort::Parity(4));
            break;

        }
        //流控
        serial->setFlowControl(QSerialPort::NoFlowControl);

        if(!serial->open(QIODevice::ReadWrite))
        {
            QMessageBox::about(this, "错误","串口打开失败");
            return;
        }
        ui->usart_name->setEnabled(false);
        ui->usart_datalengh->setEnabled(false);
        ui->usart_stop->setEnabled(false);
        ui->usart_check->setEnabled(false);
        ui->usart_bute->setEnabled(false);
        ui->pushButton->setText("关闭串口");

    }
    else
    {
        serial->close();
        ui->usart_name->setEnabled(true);
        ui->usart_datalengh->setEnabled(true);
        ui->usart_stop->setEnabled(true);
        ui->usart_check->setEnabled(true);
        ui->usart_bute->setEnabled(true);
        ui->pushButton->setText("打开串口");


    }
}

void  control::on_pushButton_5_clicked(void)
{

    if(tcp_state)
    {
         qtcpsocket->write("@rtc/5a747f1b .");
    }
    if(serial->isOpen()==true)
    {
        serial->write("@rtc/5a747f1b .");
    }

    time->show();

}

void control::on_pushButton_4_clicked()
{
    if(serial->isOpen()==true)
    {
        serial->write("@gpio98e343ca .");
    }
    if(tcp_state)
    {
     qtcpsocket->write("@gpio98e343ca .");
    }

    gpiox->show();

}

void control::on_pushButton_6_clicked()
{
    if(serial->isOpen()==true)
    {
     serial->write("@close .");
    }
    if(tcp_state)
    {
     qtcpsocket->write("@close .");
    }
}

void control::on_pushButton_7_clicked()
{
    if(serial->isOpen()==true)
    {
         serial->write("@rng/67fd8b49 .");
    }
    if(tcp_state )
    {
     qtcpsocket->write("@rng/67fd8b49 .");
    }
}

void control::on_pushButton_8_clicked()
{

    if(serial->isOpen()==true)
    {
        serial->write("@adc/0b7719a1 .");
    }
    if(tcp_state)
    {
      qtcpsocket->write("@adc/0b7719a1 .");
    }

    adcx->show();


}



void control::on_pushButton_2_clicked(bool checked)
{
    const char *  server_id;

    server_id = ui->lineEdit->text().toUtf8();

    qDebug() << ui->lineEdit->text().toUtf8() << endl;


    if(checked)
    {
            //连接服务器,指定服务端
         qtcpsocket->connectToHost(QHostAddress(server_id), 5001);

    }

   else
    {

        tcp_state = 0;
    }

}

void control::on_pushButton_9_clicked()
{

    if(serial->isOpen()==true)
    {
        serial->write("@tim/47c085b7 .");
    }
    if(tcp_state)
    {
      qtcpsocket->write("@tim/47c085b7 .");
    }
    timpwmx->show();

}

void control::rng_play()
{

    char * buff_p = NULL;
    if(serial->isOpen()==true)
    {
        buff_p = strstr(getcmd,"DATA");


        if(buff_p != NULL)
        {
            buff_p+=4;
            ui->RNG_DATA->setText(buff_p);
        }
    }


    if(tcp_state == 1)
    {
        buff_p = strstr(TCP_buff,"DATA");

        if(buff_p != NULL)
        {
            buff_p+=4;
            ui->RNG_DATA->setText(buff_p);
        }
    }

}













void control::on_pushButton_10_clicked()
{
    if(serial->isOpen()==true)
    {
        serial->write("@can/9db7b819 .");
    }
    if(tcp_state)
    {
      qtcpsocket->write("@can/9db7b819 .");
    }
    canx->show();

}

void control::on_FLASH_SELECT_clicked()
{

}

void control::on_pushButton_13_clicked()
{
    if(serial->isOpen()==true)
    {
        serial->write("@ap3216c/d465795a .");
    }
    if(tcp_state)
    {
      qtcpsocket->write("@ap3216c/d465795a .");
    }
    ap3216c->show();
}

void control::on_pushButton_14_clicked()
{

    if(serial->isOpen()==true)
    {
        serial->write("@sd_flash8fa2e5e1 .");

    }
    if(tcp_state)
    {
      qtcpsocket->write("@sd_flash8fa2e5e1 .");
    }
    sdflash->show();
}











